import pandas as pd
import os
from threading import Thread


def transform(type):
    print('process %s data -------------' % (type))
    src = '%s/time' % (type)
    dst = '%s/stock' % (type)

    if not os.path.exists(dst):
        os.makedirs(dst)

    for filename in os.listdir(src):
        print('parse ', filename)
        df = pd.read_csv('%s/%s' % (src, filename), dtype={'代码': str})
        for index, row in df.iterrows():
            code = row[1]
            name = row[2]
            stock_csv = '%s/%s.csv' % (dst, code)
            print(stock_csv)

            if int(code) >= 800000 or 'ST' in name:
                continue

            row['time'] = filename.split('.')[0]

            if not os.path.exists(stock_csv):
                stock_df = pd.DataFrame(columns=df.columns)
                stock_df['time'] = 0
            else:
                stock_df = pd.read_csv(stock_csv, dtype={'代码': str})

            stock_df.loc[len(stock_df)] = row.values
            stock_df.to_csv(stock_csv, index=False)

thread1 = Thread(target=transform, kwargs={'type': 'flow'})
thread1.start()

thread2 = Thread(target=transform, kwargs={'type': 'real'})
thread2.start()

thread1.join()
thread2.join()